<!-- <template>
  <div>
    <el-input>
      <i slot="suffix" class="el-input__icon el-icon-date"></i>
    </el-input>
  </div>
</template> -->
<script>
import CryptoJS from "crypto-js";
import Template from "../../../../../assets/utils/initData/template";

const key = CryptoJS.enc.Utf8.parse("d779506dc877d686"); //16位
const iv = CryptoJS.enc.Utf8.parse("1234567890000000");

export default {
  methods: {
    //aes加密
    encrypt(word) {
      let encrypted = "";
      if (typeof word == "string") {
        const srcs = CryptoJS.enc.Utf8.parse(word);
        encrypted = CryptoJS.AES.encrypt(srcs, key, {
          iv: iv,
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.Pkcs7
        });
      } else if (typeof word == "object") {
        //对象格式的转成json字符串
        const data = JSON.stringify(word);
        const srcs = CryptoJS.enc.Utf8.parse(data);
        encrypted = CryptoJS.AES.encrypt(srcs, key, {
          iv: iv,
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.Pkcs7
        });
      }
      return encrypted.ciphertext.toString();
    },
    // aes解密
    decrypt(word) {
      const encryptedHexStr = CryptoJS.enc.Hex.parse(word);
      const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
      const decrypt = CryptoJS.AES.decrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      });
      const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
      return decryptedStr.toString();
    }
  },
  mounted() {
    console.log(CryptoJS);
    let json = { "birthday": "1990-03-07", "tel": "17020702086", "sex": 2, "cname": "潘小芬", "doctype": 1, "idcard": "500101199003072642", "mxid": "s2KWASW0AABUjDQB", "date": "2022-10-12", "pid": "KgIAAA", "Ftime": 1, "guid": "" };
    json = JSON.stringify(json);
    json = this.encrypt(json)
    console.log('加密: ' + json);
    json = this.decrypt(json)
    console.log('解密: ' + json);
  },
  render(h) {
    let slots = { suffix: () => <i class="el-input__icon el-icon-date"></i> }
    return (
      <el-input>
        {{ append: () => (<span>213213</span>) }}
      </el-input>
    )
  },
};
</script>